package com.smartthinking.destinomusical.adapter;

//Need the following import to get access to the app resources, since this
//class is in a sub-package.
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.provider.MediaStore.Video.Thumbnails;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartthinking.destinomusical.R;

public class VideolistAdapter extends BaseAdapter  {
	private LayoutInflater inflater;
	private String path;
	List<String> mdataObjectList = new ArrayList<String>();

	public VideolistAdapter(Activity a, String[] dataObjectList, String path) {
		this.path = path;
		for (int i = 0; i < dataObjectList.length; i++) {
			mdataObjectList.add(dataObjectList[i]);
		}

		inflater = (LayoutInflater) a
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mdataObjectList == null ? 0 : mdataObjectList.size();
	}

	@Override
	public Object getItem(int position) {
		return mdataObjectList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final String obj = (String) mdataObjectList.get(position);
		View vi = convertView;
		ViewHolder holder;
		if (vi == null) {
			vi = inflater.inflate(R.layout.listitem_videolist, null);
			holder = new ViewHolder();
			holder.title = (TextView) vi.findViewById(R.id.title);
			holder.eliminar = (ImageView) vi.findViewById(R.id.eliminar);
			holder.thumbnail = (ImageView) vi.findViewById(R.id.thumbnail);
			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();
		holder.title.setText(obj);
		holder.eliminar.setTag(obj);
		holder.eliminar.setId(position);
		holder.eliminar.setOnClickListener(delete);
		Bitmap bmThumbnail;
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(path+obj, Thumbnails.MICRO_KIND);
        holder.thumbnail.setImageBitmap(bmThumbnail);
		return vi;
	}

	OnClickListener delete = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String path = Environment.getExternalStorageDirectory()
					+ "/DestinoMusical/";
			File file = new File(path + v.getTag());
			boolean deleted = file.delete();
			if (deleted) {
				mdataObjectList.remove(v.getId());
				notifyDataSetChanged();
			}
		}
	};

	public static class ViewHolder {
		public TextView title;
		public ImageView eliminar;
		public ImageView thumbnail;

	}

}